<?php

namespace App\Libs\Company\SheetTemplateHandler\Aggregate;

use App\Libs\Company\SheetTemplateHandler\Aggregate\Template221;
use App\Models\SheetTemplate;

class Template223 extends AbstractTemplate
{
    protected $lastMonthRealData;

    protected $lastYearMonthRealData;

    public function generateAbstract()
    {
        $this->renderHeader();

        $monthData = [
            'A' => ['预计' => 0, '实际数' => 0],
            'B' => ['预计' => 0, '实际数' => 0],
            'C' => ['预计' => 0.0, '实际数' => 0.0],
            'D' => ['预计' => 0.0, '实际数' => 0.0],
        ];

        foreach ($this->task->subTasks as $subTask) {
            $cellData = $subTask->data['sheets']['sheet-01']['cellData'];

            $monthData['A']['预计'] += (int) $cellData[7][2]['v'];
            $monthData['A']['实际数'] += (int) $cellData[7][3]['v'];
            $monthData['B']['预计'] += (int) $cellData[8][2]['v'];
            $monthData['B']['实际数'] += (int) $cellData[8][3]['v'];
            $monthData['C']['预计'] += (float) $cellData[10][2]['v'];
            $monthData['C']['实际数'] += (float) $cellData[10][3]['v'];
            $monthData['D']['预计'] += (float) $cellData[11][2]['v'];
            $monthData['D']['实际数'] += (float) $cellData[11][3]['v'];
        }

        $template221Handler = new Template221(SheetTemplate::find(221), $this->task);
        $this->lastMonthRealData = $template221Handler->getLastMonthData();
        $this->lastYearMonthRealData = $template221Handler->getLastYearMonthData();

        $cellData = &$this->output['sheets']['sheet-01']['cellData'];
        $cellData[4][5] = ['s' => 'NORMAL', 'v' => "统计周期：{$this->task->year}月{$this->task->key}月"];

        $cellData[7][2] = ['v' => $monthData['A']['预计'], 't' => 2];
        $cellData[7][6] = ['v' => $monthData['A']['实际数'], 't' => 2];

        $cellData[8][2] = ['v' => $monthData['B']['预计'], 't' => 2];
        $cellData[8][6] = ['v' => $monthData['B']['实际数'], 't' => 2];

        $cellData[10][2] = ['v' => $monthData['C']['预计'], 't' => 2];
        $cellData[10][6] = ['v' => $monthData['C']['实际数'], 't' => 2];

        $cellData[11][2] = ['v' => $monthData['D']['预计'], 't' => 2];
        $cellData[11][6] = ['v' => $monthData['D']['实际数'], 't' => 2];

        if (empty($this->lastMonthRealData)) {
            $cellData[7][4] = ['v' => $cellData[7][2]['v'], 't' => 2];
            $cellData[8][4] = ['v' => $cellData[8][2]['v'], 't' => 2];
            $cellData[6][4]['v'] = (int) $cellData[7][2]['v'] + (int) $cellData[8][2]['v'];

            $cellData[10][4] = ['v' => $cellData[10][2]['v'], 't' => 2];
            $cellData[11][4] = ['v' => $cellData[11][2]['v'], 't' => 2];
            $cellData[9][4]['v'] = (float) $cellData[10][2]['v'] + (float) $cellData[11][2]['v'];
        } else {
            $cellData[7][4] = ['v' => (int) $cellData[7][2]['v'] + (int) $this->lastMonthRealData['2、接待国内游客']['2']['v'] - (int) $this->lastMonthRealData['其中：一日游游客']['2']['v'], 't' => 2];
            $cellData[8][4] = ['v' => (int) $cellData[8][2]['v'] + (int) $this->lastMonthRealData['其中：一日游游客']['2']['v'], 't' => 2];

            $cellData[10][4] = ['v' => (float) $cellData[10][2]['v'] + (float) $this->lastMonthRealData['2、国内旅游收入']['2']['v'] - (float) $this->lastMonthRealData['其中：一日游游客收入']['2']['v'], 't' => 2];
            $cellData[11][4] = ['v' => (float) $cellData[11][2]['v'] + (float) $this->lastMonthRealData['其中：一日游游客收入']['2']['v'], 't' => 2];
        }

        if (empty($this->lastYearMonthRealData)) {
            $cellData[7][3] = ['v' => 0, 't' => 2];
            $cellData[7][5] = ['v' => 0, 't' => 2];

            $cellData[8][3] = ['v' => 0, 't' => 2];
            $cellData[8][5] = ['v' => 0, 't' => 2];

            $cellData[6][3] = ['v' => 0, 't' => 2];
            $cellData[6][5] = ['v' => 0, 't' => 2];

            $cellData[10][3] = ['v' => 0, 't' => 2];
            $cellData[10][5] = ['v' => 0, 't' => 2];

            $cellData[11][3] = ['v' => 0, 't' => 2];
            $cellData[11][5] = ['v' => 0, 't' => 2];

            $cellData[10][3] = ['v' => 0, 't' => 2];
            $cellData[10][5] = ['v' => 0, 't' => 2];
        } else {
            $sheetRow = 8;
            $lastMonthValue = (int) $this->lastYearMonthRealData['2、接待国内游客'][2]['v'] - (int) $this->lastYearMonthRealData['其中：一日游游客'][2]['v'];
            $lastMonthTotal = (int) $this->lastYearMonthRealData['2、接待国内游客'][4]['v'] - (int) $this->lastYearMonthRealData['其中：一日游游客'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];

            $sheetRow = 9;
            $lastMonthValue = $this->lastYearMonthRealData['其中：一日游游客'][2]['v'];
            $lastMonthTotal = $this->lastYearMonthRealData['其中：一日游游客'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];

            $sheetRow = 7;
            $lastMonthValue = $this->lastYearMonthRealData['2、接待国内游客'][2]['v'];
            $lastMonthTotal = $this->lastYearMonthRealData['2、接待国内游客'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];

            $sheetRow = 11;
            $lastMonthValue = (float) $this->lastYearMonthRealData['2、国内旅游收入'][2]['v'] - (float) $this->lastYearMonthRealData['其中：一日游游客收入'][2]['v'];
            $lastMonthTotal = (float) $this->lastYearMonthRealData['2、国内旅游收入'][4]['v'] - (float) $this->lastYearMonthRealData['其中：一日游游客收入'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];

            $sheetRow = 12;
            $lastMonthValue = $this->lastYearMonthRealData['其中：一日游游客'][2]['v'];
            $lastMonthTotal = $this->lastYearMonthRealData['其中：一日游游客'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];

            $sheetRow = 10;
            $lastMonthValue = $this->lastYearMonthRealData['2、国内旅游收入'][2]['v'];
            $lastMonthTotal = $this->lastYearMonthRealData['2、国内旅游收入'][4]['v'];
            $cellData[$sheetRow - 1][3] = ['f' => "=IFERROR(((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}-MOD((C{$sheetRow}-{$lastMonthValue})/{$lastMonthValue}, 0.0001))*100,0)", 't' => 2];
            $cellData[$sheetRow - 1][5] = ['f' => "=IFERROR(((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}-MOD((E{$sheetRow}-{$lastMonthTotal})/{$lastMonthTotal}, 0.0001))*100,0)", 't' => 2];
        }

        return $this->output;
    }
}
